package MyHashSet;

import java.util.Iterator;
import java.util.LinkedList;

public class Solution {
    class MyHashSet {
        private static final int base = 769;
        LinkedList<Integer>[]table = new LinkedList[base];
        public MyHashSet() {
            for (int i = 0; i < table.length; i++) {
                table[i] = new LinkedList<>();
            }
        }

        public void add(int key) {
            int h = hash(key);
            Iterator<Integer> iterator = table[h].iterator();
            while (iterator.hasNext()) {
                Integer element = iterator.next();
                if (element == key) {
                    return;
                }
            }
            table[h].offerLast(key);
        }

        public void remove(int key) {
            int h = hash(key);
            Iterator<Integer> iterator = table[h].iterator();
            while(iterator.hasNext()){
                Integer element = iterator.next();
                if(element == key){
                    table[h].remove(key);
                    return;
                }
            }
        }

        public boolean contains(int key) {
            int h = hash(key);
            Iterator<Integer> iterator = table[h].iterator();
            while(iterator.hasNext()){
                Integer element = iterator.next();
                if(element == key){
                    return true;
                }
            }
            return false;
        }
        private static int hash(int key){
            return key%base;
        }
    }
}
